<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="100px">
      <el-form-item label="账号名" prop="accountName">
        <el-input
          v-model="queryParams.accountName"
          placeholder="请输入账号名"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="用户id" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户id"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="昵称" prop="nick">
        <el-input
          v-model="queryParams.nick"
          placeholder="请输入昵称"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="渠道ID" prop="channelId">
        <el-input
          v-model="queryParams.channelId"
          placeholder="请输入渠道ID"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:catUserAccount:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:catUserAccount:export']"
        >导入</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="catUserAccountList" @selection-change="handleSelectionChange">
      <!-- <el-table-column label="id" align="center" prop="id" /> -->
      <el-table-column label="注册时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="注册渠道" align="center" prop="channelId" />
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="用户账号" align="center" prop="accountName" />
      <el-table-column label="用户昵称" align="center" prop="nick" />
      <el-table-column label="会员等级" align="center" prop="userLevel" />
      <el-table-column label="猫币余额" align="center" prop="catCoinBalance" />
      <el-table-column label="猫粮余额" align="center" prop="balance" />
      <el-table-column label="宠爱值余额" align="center" prop="petValueBalance" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          {{ scope.row.status===1?"未注册":scope.row.status===2?"正常":scope.row.status===3?"已注销":'已删除' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="250px">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleInfo(scope.row)"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:catUserAccount:edit']"
          >编辑</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleUpdateNumbers(scope.row.id)"
          >修改数值</el-button>
          <el-button v-if="scope.row.status === 2"
            size="mini"
            type="text"
            @click="handleStatus(scope.row.id, 3,'注销')"
          >注销</el-button>
          <el-button 
            v-if="scope.row.status === 3"
            size="mini"
            type="text"
            @click="handleStatus(scope.row.id, 2,'恢复')"
          >恢复</el-button>
          <el-button
            v-if="scope.row.status !== -1"
            size="mini"
            type="text"
            @click="handleStatus(scope.row.id, -1,'删除')"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <info v-if="infoVisible" ref="info" />

    <updateNumbers v-if="updateNumbersVisible" ref="updateNumbers" @getList='getList'/>

    <!-- 添加或修改用户注册的账号对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="40%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="手机号码" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" maxlength="11"/>
        </el-form-item>
        <el-form-item label="用户昵称" prop="nick">
          <el-input v-model="form.nick" placeholder="请输入昵称" />
        </el-form-item>
        <el-form-item label="性别">
          <el-select v-model="form.sex" placeholder="请选择性别">
            <el-option label="男" :value="1" />
            <el-option label="女" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="个性签名" prop="signature">
          <el-input v-model="form.signature" placeholder="请输入个性签名" />
        </el-form-item>
        <div v-if="form.id">
          <el-form-item label="旧密码" prop="password">
            <el-input v-model="form.password" placeholder="请输入账号密码" :disabled="true"/>
          </el-form-item>
          <el-form-item label="设置新密码">
            <el-input v-model="form.newPassword1"/>
          </el-form-item>
          <el-form-item label="确认密码">
            <el-input v-model="form.newPassword2"/>
          </el-form-item>
        </div>
        <el-form-item v-else label="账号密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入账号密码"/>
        </el-form-item>

        <div v-if="form.userImAuthority">
          <el-form-item label="权限管理">
            <el-checkbox v-model="form.userImAuthority.chatEnable" label="禁止聊天" :true-label='1' :false-label='0' />
            <el-checkbox v-model="form.userImAuthority.envelopeEnable" label="禁止发红包" :true-label='1' :false-label='0' />
            <el-checkbox v-model="form.userImAuthority.chatroomEnable" label="禁止进入聊天室" :true-label='1' :false-label='0' />
            <el-checkbox v-model="form.userImAuthority.followingEnable" label="禁止关注用户" :true-label='1' :false-label='0' />
            <el-checkbox v-model="form.userImAuthority.giveEnable" label="禁止赠送钥匙" :true-label='1' :false-label='0' />
            <el-checkbox v-model="form.userImAuthority.subscribeEnable" label="禁止订阅房间" :true-label='1' :false-label='0' />
          </el-form-item>

          <el-form-item label="时间限制">
              <el-date-picker v-model="form.userImAuthority.timeLimit" 
              type="datetime" 
              placeholder="选择日期时间"
              format="yyyy-MM-dd HH:mm:ss"
              value-format="yyyy-MM-dd HH:mm:ss" />
          </el-form-item>
        </div>
        
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCatUserAccount,updateByStatus,
 getCatUserAccount, delCatUserAccount, addCatUserAccount, updateCatUserAccount, exportCatUserAccount } from "@/api/system/catUserAccount";
import info from "./info"
import updateNumbers from "./updateNumbers"
export default {
  name: "CatUserAccount",
  components:{
    info,
    updateNumbers
  },
  data() {
    return {
      infoVisible:false,
      updateNumbersVisible:false,

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 用户注册的账号表格数据
      catUserAccountList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        accountName: undefined,
        userId: undefined,
        nick: undefined,
        title: undefined,
        sex: undefined,
        balance: undefined,
        petValueBalance: undefined,
        lightStick: undefined,
        channel: undefined,
        device: undefined,
        signature: undefined,
        labelList: undefined,
        albumList: undefined,
        area: undefined,
        userLevel: undefined,
        avatar: undefined,
        isOnline: undefined,
        catCode: undefined,
        age: undefined,
        channelId: undefined,
        birthDay: undefined,
        phone: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        isOnline: [
          { required: true, message: "是否在线不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
   
  },
  methods: {
    /** 查询用户注册的账号列表 */
    getList() {
      this.loading = true;
      listCatUserAccount(this.queryParams).then(response => {
        this.catUserAccountList = response.rows;
        this.total =  parseInt(response.total) ;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        accountName: undefined,
        userId: undefined,
        password: undefined,
        newPassword1: undefined,
        newPassword2: undefined,
        createTime: undefined,
        status: "0",
        nick: undefined,
        title: undefined,
        sex: undefined,
        balance: undefined,
        petValueBalance: undefined,
        lightStick: undefined,
        channel: undefined,
        device: undefined,
        signature: undefined,
        labelList: undefined,
        albumList: undefined,
        area: undefined,
        userLevel: undefined,
        avatar: undefined,
        isOnline: undefined,
        catCode: undefined,
        age: undefined,
        channelId: undefined,
        birthDay: undefined,
        phone: undefined,
        userImAuthority:{}
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户注册的账号";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids

      // 1290680031068151810
      console.log("handleUpdate row= %o", row)
      console.log("handleUpdate id = %o", id)
      getCatUserAccount(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户注册的账号";
      });
    },
    /** 修改数值 */
    handleUpdateNumbers(id) {
      this.updateNumbersVisible = true;
      this.$nextTick(() => {
        this.$refs.updateNumbers.init(id)
      })
    },
    /* 详情 */
    handleInfo(row){
      this.infoVisible = true
      this.$nextTick(() => {
        this.$refs.info.init(row)
      })
    },
    handleStatus(id,status, text){
      const data ={
        id:id,
        status: status
      }
      updateByStatus(data).then(response => {
        if (response.code === 200) {
          this.msgSuccess(`${text}成功`);
          this.getList();
        }
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      if(this.form.newPassword1 && this.form.newPassword2){
        if(this.form.newPassword1 === this.form.newPassword2){
          this.form.newPassword = this.form.newPassword2
        }else{
          this.msgError("新密码设置不一致");
        }
      }

      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateCatUserAccount(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addCatUserAccount(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除用户注册的账号编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCatUserAccount(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有用户注册的账号数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportCatUserAccount(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>